/**项目地址**/
var domainUrl = "https://passport.spider.com.cn";
//手机号格式
var mobilereg=/^1[3|4|5|6|7|8|9]\d{9}$/;
/**倒计时时长**/
var mobilecodetime = 100;

var myKey = "sp3";
var mySign = "a25$54%589@65d\#Abd\#fh$aS%dfe";

/**生成uuid**/
function guid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
        return v.toString(16);
    });
}
/**处理uuid**/
function generalUUID(){
	var uuid = guid().replace(/-/g, "");
	/**写入cookie**/
	$.cookie('spiderGuid', null);
	$.cookie('spiderGuid', uuid);
	return uuid;
}

/**获取时间戳**/
function gTimeStamp(){
	var timeStamp = new Date().getTime();
	return timeStamp;
}

/**实现hashMap**/
function HashMap(){
    this.map = {};
}
HashMap.prototype = {
    put : function(key , value){
        this.map[key] = value;
    },
    get : function(key){
        if(this.map.hasOwnProperty(key)){
            return this.map[key];
        }
        return null;
    },
    remove : function(key){
        if(this.map.hasOwnProperty(key)){
            return delete this.map[key];
        }
        return false;
    },
    removeAll : function(){
        this.map = {};
    },
    keySet : function(){
        var _keys = [];
        for(var i in this.map){
            _keys.push(i);
        }
        return _keys;
    }
};
HashMap.prototype.constructor = HashMap;

/**加载图片验证码**/
function loadPicCode(busibessType){
	var url_remote = domainUrl + "/user/imageVerifyCode.action";
	var timeStamp = gTimeStamp();
	var randomCode = generalUUID();
	var preSign = "businesstype" + busibessType + "key" + myKey + "randomCode" + randomCode + "timeStamp" + timeStamp + mySign;
	var sign = md5(preSign);
	$.ajax({
		type : 'POST',
		url : url_remote,
		dataType : 'json',
		data:{
			"businesstype":busibessType,
			"randomCode":randomCode,
			"timeStamp":timeStamp,
			"sign":sign,
			"key":myKey
		},
		success : function(data) {
			var dataResult = data.result;
			if('0' == dataResult){
				$("#verifycode").attr("src","data:image/jpg;base64,"+data.data.imageCode);
			}else{
				
			}
		},
		error : function() {
			return;
		}
	});
}

/**通用发送验证码**/
function commonSendSms(tipid,mobileid,bussnessType){
	var mobile = $("#" + mobileid).val();
	if(null == mobile || '' == mobile){
		$("#"+tipid).html("请输入手机号");
		return;
	}
	sendCoreProcess("sdbutton",tipid,mobile,bussnessType,"0","","n",'2');
}

/**注册发送短信验证码**/
function registAccountSendSms(tipid,mobileid){
	var mobile = $("#" + mobileid).val();
	var freshcode = $("#reshcode").val();
	if(null == mobile || '' == mobile){
		$("#"+tipid).html("请输入手机号");
		$("#phoneNum").focus();
		return;
	}else if(!mobilereg.test(mobile)){
		$("#"+tipid).html("请检查您输入的手机");
		$("#phoneNum").focus();
		return;
	}else if(null == freshcode || '' == freshcode){
		$("#phoneNum_tip").html("请输入验证码");
		$("#reshcode").focus();
		return;
	}
	/**倒计时结束后给按钮赋值**/
	var changeContent = "<a href=\"javascript:void(0)\" onclick=\"registAccountSendSms('phoneNum_tip','phoneNum');\" id=\"btngetmobilecode\" class=\"btn_gray2 btnGet black1 f14\">获取短信动态码</a>";
	sendCoreProcess("sendsmsbutton",tipid,mobile,"newRegist","1",changeContent,"y",'1');
}

/**找回密码获取手机验证码**/
function findPdSendSms(tipid,mobileid){
	var mobile = $("#" + mobileid).val();
	if(null == mobile || '' == mobile || !mobilereg.test(mobile)){
		$("#"+tipid).html("手机号有误，请重新按照流程找回密码");
	}else{
		/**倒计时结束后给按钮赋值**/
		var changeContent = "<a href=\"javascript:void(0)\" onclick=\"findPdSendSms('dtmNum_tip','phoneNum');\" id=\"btngetmobilecode\" class=\"btn_gray2 btnGet black1 f14\">获取短信动态码</a>";
        sendCoreProcess("sendsmsbutton",tipid,mobile,"newForgetPd","0",changeContent,"n",'1');
	}
}

var getmobilecodehander = null;
/**参数依次为 按钮id、提示框id、手机号、业务类型、是否需要图片验证码、按钮按下后改变的内容、验证不通过是否刷新图片验证码、倒计时执行函数类型**/
function sendCoreProcess(sendsmsbutton,tipid,mobile,busibessType,needImageCode,changeContent,freshPicflag,processType){
	$("#dtmNum_tip").html("");
	var reqUrl = domainUrl + "/user/sendPhoneVerifyCode.action";
	/**时间戳**/
	var timeStamp = gTimeStamp();
	var preSign = "businesstype" + busibessType + "key" + myKey + "mobile" + mobile + "needImageCode" + needImageCode + "timeStamp" + timeStamp + mySign;
	if("newRegist" == busibessType){
		var randomCode = $.cookie('spiderGuid');
		var inputVerifyCode = $("#reshcode").val();
		preSign = "businesstype" + busibessType + "key" + myKey + "mobile" + mobile + "needImageCode" + needImageCode + "randomCode" + randomCode + "timeStamp" + timeStamp + "verifyCode" + inputVerifyCode + mySign;
	}
	var sign = md5(preSign);
	$.ajax({
		type : 'POST',
		url : reqUrl,
		dataType : 'json',
		data:{
			"mobile":mobile,
			"businesstype":busibessType,
			"needImageCode":needImageCode,
			"timeStamp":timeStamp,
			"sign":sign,
			"randomCode":randomCode,
			"verifyCode":inputVerifyCode,
			"key":myKey
		},
		success : function(data) {
			var dataResult = data.result;
			if('0' == dataResult){
				/**倒计时执行函数**/
				if("1" == processType){
					intervalProcessType1(tipid,sendsmsbutton,changeContent);
				}else{
					intervalProcessType2(tipid,sendsmsbutton);
				}
			}else{
				/**异常信息处理**/
				var dataMessage = data.message;
				if("F1001" == dataMessage || "F1002" == dataMessage || "F1005" == dataMessage){
					$("#"+tipid).html("系统繁忙，请稍后再试");
				}else{
					/**绑定手机和修改绑定手机，如果手机已绑定，则替换提示**/
					if(("newBindPhone" == busibessType || "newModifyVerifyNewPhone" == busibessType) && "F1009" == dataMessage){
						$("#"+tipid).html("你输入的手机号已经被其他账户绑定，建议更换其它号码再试，谢谢");
					}else{
						$("#"+tipid).html(data.description);
					}
				}
			}
			if("y" == freshPicflag){
				/**重新加载图片验证码**/
				loadPicCode(busibessType);
			}
		},
		error : function() {
			alert("网页错误，请刷新后再试");
			return;
		}
	});
}

/**倒计时类型1**/
function intervalProcessType1(tipid,sendsmsbutton,changeContent){
	$("#"+tipid).html("验证码已发送请查收");
	getmobilecodehander = setInterval(function() {
	   	if(mobilecodetime <= 0){
            window.clearInterval(getmobilecodehander);
            getmobilecodehander = null;
			$("#" + sendsmsbutton).html(changeContent);
            mobilecodetime = 100;
            $("#" + tipid).html("");
	   	 }else{
	   		$("#" + sendsmsbutton).html("<span class=\"btn_gray2 f14\">重新获取短信动态码（<em>" + mobilecodetime-- + "</em>）</span>");
	   	 }
     }, 1000);
}

/**倒计时类型2**/
function intervalProcessType2(tipid,sendsmsbutton){
	$("#"+tipid).html("验证码已发送请查收");
	getmobilecodehander = setInterval(function() {
	   	if(mobilecodetime <= 0){
            window.clearInterval(getmobilecodehander);
            getmobilecodehander = null;
            mobilecodetime = 100;
            $("#" + tipid).html("");
            document.getElementById(sendsmsbutton).disabled = "";
			document.getElementById(sendsmsbutton).value = "获取验证码";
			var aa = document.getElementById("'" + sendsmsbutton + "'");
			aa.style.cursor = "pointer";
	   	 }else{
	   	 	document.getElementById(sendsmsbutton).disabled = "disabled";
			document.getElementById(sendsmsbutton).value = "获取验证码(" + mobilecodetime + ")";
			var aa = document.getElementById(sendsmsbutton);
			aa.style.cursor = "default";
			mobilecodetime --;
	   	 }
     }, 1000);
}

